package lvbu.wang.lvbuoperator;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.content.FileProvider;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import java.io.File;
import java.util.List;

import lvbu.wang.lvbuoperator.constants.ConstantsValue;
import lvbu.wang.lvbuoperator.core.App;
import lvbu.wang.lvbuoperator.ui.index.IotIndexActivity;

public class Quick {

    public static void startQRCode(final Activity ac, final int requestCode) {
        AndPermission.with(ac).runtime()
                .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
                .onGranted(data -> {
                    Intent intent = new Intent(ac, CaptureActivity.class);
                    ZxingConfig config = new ZxingConfig();
                    config.setShowAlbum(false);
                    config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                    intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                    ac.startActivityForResult(intent, requestCode);
                }).onDenied(data -> AndPermission.with(ac).runtime().setting().start(1001)).start();
    }


    public static Intent getInstallAppIntent(final File file, final String authority, final boolean isNewTask) {
        if (file == null) return null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        String type = "application/vnd.android.package-archive";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            data = Uri.fromFile(file);
        } else {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            data = FileProvider.getUriForFile(App.getInstance(), authority, file);
        }
        intent.setDataAndType(data, type);
        return getIntent(intent, isNewTask);
    }

    private static Intent getIntent(final Intent intent, final boolean isNewTask) {
        return isNewTask ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) : intent;
    }



    public static Boolean isBleOpen(){
        return BluetoothAdapter.getDefaultAdapter().isEnabled();
    }

    public static void sysOpenBle(Activity ac) {
        ac.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 8897);
    }

}
